package com.weizhizs.assistant.code;

import com.weizhizs.assistant.code.dao.GeneratorDao;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.StringUtils;
import lombok.Data;
import com.weizhizs.assistant.code.config.ParamConfig;

import java.util.Map;

@Data
public class GeneratorRunner {

    public void generate() {
        Map<String, ICodeGenerator> generators = GeneratorRegister.codeGenerators();

        Map<String, String> tableList = GeneratorDao.getInstance().getTableList(
                ParamConfig.getInstance().getPreTableName(),
                ParamConfig.getInstance().getPreFilterName());

        if (tableList == null || tableList.size() <= 0) {
            System.out.println("tableList is null.");
            return ;
        }

        for(String tableName : tableList.keySet()) {
            TableVO tableVO = new TableVO();

            tableVO.setTableName(tableName);
            tableVO.setTableComment(tableList.get(tableName));

            String className = TableHelper.tableNameToClassName(tableName);
            tableVO.setClassName(StringUtils.firstToLowerCase(className));

            boolean havePk = GeneratorDao.getInstance().loadTableColumns(tableVO);
            if (!havePk) {
                System.out.println(tableName + " not key!!!!!!!!!!");
                continue;
            }
            for (ICodeGenerator generator : generators.values()) {
                generator.run(tableVO);
            }
        }
    }

}
